#include "updateview.h"
#include "dcuinformation.h"
#include <QDebug>
#include <QLabel>

UpdateView::UpdateView(QWidget *parent) : QWidget(parent)
{
    setWindowFlags(Qt::FramelessWindowHint);
    setAutoFillBackground(true);
    QPalette background = palette();
    background.setBrush(QPalette::Window, QBrush(QPixmap(":/images/defaultUI.png")));
    setPalette(background);

    progressBar = new QProgressBar(this);
    progressBar->setValue(0);
    progressBar->setMinimum(0);
    progressBar->setMaximum(100);
    progressBar->setGeometry(QRect(250, 160, 400, 50));

    QLabel *tipText = new QLabel(this);
    tipText->setText("请勿断电，正在升级...");
    tipText->setFont(QFont(ZITI, 20, QFont::Bold));
    tipText->setAlignment(Qt::AlignCenter);
    tipText->setGeometry(QRect(200, 100, 450, 50));
    tipText->setStyleSheet("color:red;background-color:yellow");

    logtext = new QTextEdit(this);
    logtext->setGeometry(QRect(250, 220, 350, 150));
    logtext->setAlignment(Qt::AlignLeft);
    logtext->setFont(QFont(ZITI, 13, QFont::Bold));
    logtext->setStyleSheet("color:white");
    logtext->setTextColor(QColor("black"));
    logtext->setReadOnly(true);
}

void UpdateView::msgBoxShow(QString msg)
{
    if (!msgBox.isVisible())
    {
        msgBox.setText(msg);
        msgBox.setFont(QFont(ZITI, 20, QFont::Bold));
        msgBox.setStyleSheet("color:red;text-align:center;");
        msgBox.show();
    }
}

void UpdateView::msgBoxClose()
{
    if (msgBox.isVisible())
    {
        msgBox.close();
    }
}

void UpdateView::httpdownloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{
    qDebug() << bytesReceived << bytesTotal;
    if (bytesTotal)
    {
        progressBar->setValue(bytesReceived * 100 / bytesTotal);
    }
    else
    {
        logtext->append(QString("警告文件大小：%1").arg(bytesTotal));
    }
}

void UpdateView::outPutLog(QString log)
{
    logtext->append(log);
}

void UpdateView::clearLog()
{
    logtext->clear();
}
